<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博授权</title>
</head>
<body>
    <script src="/static/js/jquery.min.js"></script>
    <script>
        function getQueryString(v){
            var quert = window.location.search.substring(1);
            var data = quert.split('&');
            for (var i=0;i<data.length;i++){
                // state=123
                var par = data[i].split('=');
                // [state,123]
                if (par[0] == v){
                    return par[1]
                }
            }
            return null
        }

        $(function(){
            var code = getQueryString('code');
            $.ajax({
                type:'get',
                dataType:'json',
                url:'http://127.0.0.1:8000/v1/user/weibo/token?code='+code,
                success:function(result){
                    console.log(result)
                    // 已经注册过了 200
                    if (result.code == 200){
                        // 走正常登录流程
                        window.localStorage.setItem('xiaoqi_token', result.token);
                        window.localStorage.setItem('xiaoqi_user', result.username);
                        alert('登陆成功');
                        window.location =  '/index';
                    }else if (result.code == 10999){
                        // 还未注册过 10999 - 弹到注册页面进行注册
                        wuid = result.wuid;
                        window.location = '/register?wuid='+wuid
                    }else{
                        alter(result.error)
                    }
                }
            })
        })

    </script>
</body>
</html>